//
//  USCommentInputToolView.m
//  us
//
//  Created by karry on 2025/5/22.
//

#import "USCommentInputToolView.h"
 
@interface USCommentInputToolView ()
@property (nonatomic, assign) CGFloat keyboardHeight;
@end

@implementation USCommentInputToolView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    
        [self setupView];
    }
    return self;
}

- (void)setupView {
    self.backgroundColor = [UIColor whiteColor];

    self.sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sendButton setImage:[UIImage imageNamed:@"comment_send"] forState:0];
    [self.sendButton addTarget:self action:@selector(sendButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.sendButton];
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(82);
        make.height.mas_equalTo(50);
    }];
    
    UIView *bgView = [[UIView alloc]init];
    [self addSubview:bgView];
    bgView.backgroundColor = UIColor.mi_2_color;
    bgView.layer.cornerRadius = 14;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.bottom.mas_equalTo(-12);
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(16);
        make.right.equalTo(self.sendButton.mas_left).offset(-12);
    }];
    
    self.textField = [[UITextField alloc] init];
    [self.textField setFont:[UIFont systemFontOfSize:16]];
    self.textField.placeholder = @"说些什么吧";
    [bgView addSubview:self.textField];
    self.textField.borderStyle = UITextBorderStyleNone;
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.centerY.mas_equalTo(0);
    }];

  
    @weakify(self);
    [[self.textField.rac_textSignal distinctUntilChanged] subscribeNext:^(NSString * _Nullable text) {
        @strongify(self);
        self.sendButton.enabled = text.length > 0;
    }];
}
 
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)sendButtonTapped {
    NSString *text = self.textField.text;
    if (text.length == 0) return;

    if (self.onSend) {
        self.onSend(text);
    }

}

@end
